import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/home_api.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/lc_video_data_entity.dart';
import 'package:lcapp/model/dialog/confirm.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/page/home/home_new_video_item.dart';
import 'package:lcapp/page/home/home_video_item.dart';
import 'package:lcapp/widget/asr_app_bar.dart';

class MinVideoDetailPage extends StatefulWidget {
  final Map arguments;
  const MinVideoDetailPage({Key? key, required this.arguments}) : super(key: key);

  @override
  _MinVideoDetailPageState createState() => _MinVideoDetailPageState();
}

class _MinVideoDetailPageState extends State<MinVideoDetailPage> {

  late PageController pageController;
  List<LcVideoDataEntity> lcVideos = [];
  bool isMine = false;
  int currentIndex = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.arguments['videoId']!=null){
      getVideoDetail();
    }else{
      isMine = widget.arguments['isMine']??false;
      lcVideos = widget.arguments['lcVideos']??[];
      currentIndex =  widget.arguments['index']??0;
      pageController = PageController(initialPage: currentIndex);
    }
  }

  void getVideoDetail()async{
    ResponseData res = await HomeAPI.getVideoById(widget.arguments['videoId'].toString());
    if(res.code == 200 && res.data != null){
      LcVideoDataEntity entity = LcVideoDataEntity().fromJson(res.data);
      lcVideos = [entity];
      currentIndex =  0;
      pageController = PageController(initialPage: currentIndex);
    }
    if(mounted){
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Container(
        width: 1.sw,
        height: 1.sh,
        child: Stack(
          children: [
            Positioned(
              child: Container(
                child: PageView.builder(
                  controller: pageController,
                  scrollDirection: Axis.vertical,
                  itemCount: lcVideos.length,
                  onPageChanged: (index){
                  },
                  itemBuilder: (context, index) {
                    return Platform.isIOS ? HomeVideoItem(3,videos: lcVideos,entity: lcVideos[index],isMine:isMine,clickFun: (int type){
                      if(type==4){
                        openConfirm(
                          context,
                          isTitle: true,
                          title: '提示',
                          content: '确定删除该条作品吗',
                          succFn: () async {
                            ResponseData res = await UserAPI.deleteMinVideos(lcVideos[index].id!);
                            if(res.code==200){
                              setState(() {
                                lcVideos.removeAt(index);
                                showToast('删除成功');
                                Navigator.of(context).pop();
                              });
                            }
                          },
                        );
                      }
                    },) : HomeVideoItem(3,videos: lcVideos,entity: lcVideos[index],isMine:isMine,clickFun: (int type){
                      if(type==4){
                        openConfirm(
                          context,
                          isTitle: true,
                          title: '提示',
                          content: '确定删除该条作品吗',
                          succFn: () async {
                            ResponseData res = await UserAPI.deleteMinVideos(lcVideos[index].id!);
                            if(res.code==200){
                              setState(() {
                                lcVideos.removeAt(index);
                                showToast('删除成功');
                                Navigator.of(context).pop();
                              });
                            }
                          },
                        );
                      }
                    },);
                  },
                ),
              ),left: 0,right: 0,bottom: 0,top: 0,),
            Positioned(
              child: AsrAppBar(
                backgroundColor: Colors.transparent,
                barColor: Colors.white,
                isLead: false,
              ),
              left: 0,right: 0,top: 0,height: ScreenUtil().statusBarHeight,
            ),
            Positioned(child: Container(
              child: Row(
                children: [
                  GestureDetector(
                    onTap: (){
                      Navigator.of(context).pop();
                    },
                    child: Icon(Icons.arrow_back_ios,color: Colors.white,size: 40.w,),
                  ),
                  Expanded(child: Container())
                ],
              ),
            ),left: 30.w,top: ScreenUtil().statusBarHeight + 50.w,right: 30.w,),
          ],
        ),
      ),
    );
  }
}
